<template>
    <div>
        <div class="addClass">
            <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh" @keydown.enter.native="addRole">
            </el-input>
            <el-button  size="small" type="primary" icon="el-icon-plus" @click="addRole">添加角色</el-button>
        </div>


        <div class="collapseClass">
            <el-collapse v-model="activeName" accordion @change="change">

                <el-collapse-item :title="role.nameZh" :name="role.id" v-for="(role,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: #ff0000" type="text" icon="el-icon-delete" @click="deleteRole(role)"></el-button>
                        </div>
                        <div>
                            <el-tree
                                    ref="tree"
                                    :key="index"
                                    :default-checked-keys="selectMenus" node-key="id"
                                    show-checkbox :data="allMenus" :props="defaultProps"></el-tree>
                        <div style="display: flex;justify-content: flex-end">
                            <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                            <el-button size="mini" type="primary" @click="update(role.id,index)">确认修改</el-button>
                        </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "AdminRoleManage",
        data(){
            return {
                activeName: -1,
                role: {
                    name: '',
                    nameZh: '',
                },
                roles: [],
                allMenus: [],
                defaultProps: {
                    children: 'children',
                    label: 'name',
                },
                selectMenus: [],
            }
        },
        mounted() {
            this.initRoles();
        },
        methods:{
            deleteRole(role){
                this.$confirm('此操作会永久删除['+role.nameZh+']职位,是否继续？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    this.deleteRequest('/role/deleteRole/'+role.id).then(resp=>{
                        if(resp){
                            this.initRoles();
                        }
                    })
                }).catch(()=>{
                    this.$message({
                        type: "info",
                        message: '已取消删除',
                    })
                })
            },
            addRole(){
              if(this.role.name && this.role.nameZh){
                  this.postRequest('/role/role',this.role).then(resp=>{
                      if(resp){
                          this.initRoles();
                            this.role.name='';
                            this.role.nameZh='';
                      }
                  })
              }  else{
                  this.$message("所有字段不能为空");
              }
            },
            change(rid){
                if(rid){
                    this.initAllMenus()
                    this.initSelectMenus(rid)
                }
            },
            initAllMenus(){
                // 查询所有菜单
                this.getRequest('/system/config/menus').then(resp=>{
                    if(resp){
                        this.allMenus = resp;
                    }
                })
            },
            initRoles(){
                // 查询所有角色
                this.getRequest('/role/roles').then(resp=>{
                    if(resp){
                        this.roles = resp.data;
                    }
                })
            },
            initSelectMenus(rid){
                this.getRequest('/system/config/menus/'+rid).then(resp=>{
                    if(resp){
                        this.selectMenus = resp;
                    }
                })
                // 根据id查出来selectMenus 菜单id
            },
            cancelUpdate(){
              this.activeName = -1;
            },
            update(rid,index){
                let tree = this.$refs.tree[index];
                // console.log(tree);
                let selectKeys = tree.getCheckedKeys(true);
                // console.log(selectKeys);
                // console.log(rid);
                let url = '/system/config/updateMenuRole?rid='+rid;
                selectKeys.forEach(key=>{
                    url+='&mids='+key;
                });
                // 调用
                this.putRequest(url).then(resp=>{
                    if(resp){
                        this.activeName = -1;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .addClass{
        display: flex;
        justify-content: flex-start;
    }
    .addClass .el-input{
        width: 300px;
        margin-right: 6px;
    }
    .collapseClass{
        margin-top: 10px;
        width: 700px;
    }
</style>